﻿
Imports System.Runtime.CompilerServices
Imports osi.root.connector
Imports osi.root.delegates
Imports osi.root.lock

'according to generic_perf test,
'the performance to directly access the p should be on-par with other kind of wrapper logic
Public Class wrapper(Of T As Structure)
    Public p As T
End Class

Public Module _extension
    'formation refers lock
    <Extension()> Public Function free(ByVal i As wrapper(Of forks)) As Boolean
        k_assert(Not i Is Nothing)
        Return i.p.free()
    End Function

    <Extension()> Public Function not_free(ByVal i As wrapper(Of forks)) As Boolean
        k_assert(Not i Is Nothing)
        Return i.p.not_free()
    End Function

    <Extension()> Public Function mark_as(ByVal i As wrapper(Of forks), ByVal s As Int32) As Boolean
        k_assert(Not i Is Nothing)
        Return i.p.mark_as(s)
    End Function

    <Extension()> Public Sub mark_free(ByVal i As wrapper(Of forks))
        k_assert(Not i Is Nothing)
        i.p.mark_free()
    End Sub

    <Extension()> Public Sub release(ByVal i As wrapper(Of forks))
        k_assert(Not i Is Nothing)
        i.p.release()
    End Sub

    <Extension()> Public Function null_or_not_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        Return i Is Nothing OrElse
               i.p.not_in_use()
    End Function

    <Extension()> Public Function not_null_and_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        Return Not i Is Nothing AndAlso
               i.p.in_use()
    End Function

    <Extension()> Public Function null_or_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        Return i Is Nothing OrElse
               i.p.in_use()
    End Function

    <Extension()> Public Function not_null_and_not_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        Return Not i Is Nothing AndAlso
               i.p.not_in_use()
    End Function

    <Extension()> Public Function in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        k_assert(Not i Is Nothing)
        Return i.p.in_use()
    End Function

    <Extension()> Public Function not_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        k_assert(Not i Is Nothing)
        Return i.p.not_in_use()
    End Function

    <Extension()> Public Function mark_in_use(ByVal i As wrapper(Of singleentry)) As Boolean
        k_assert(Not i Is Nothing)
        Return i.p.mark_in_use()
    End Function

    <Extension()> Public Sub release(ByVal i As wrapper(Of singleentry))
        k_assert(Not i Is Nothing)
        i.p.release()
    End Sub

    <Extension()> Public Sub mark_not_in_use(ByVal i As wrapper(Of singleentry))
        k_assert(Not i Is Nothing)
        i.p.mark_not_in_use()
    End Sub

    <Extension()> Public Function increment(ByVal this As wrapper(Of unchecked_int),
                                            Optional ByVal that As Int32 = 1) As Int32
        k_assert(Not this Is Nothing)
        this.p += that
        Return this.p
    End Function
End Module
